package buildrobot_info

import (
	"go_server/game_server/api/service/build"
	"go_server/game_server/protocol/pb"
	"go_server/zinx/ziface"
	"go_server/zinx/zlog"
)

func (s *Buildrobot_info_service) S2C_BuildRobotLvUpOver(conn ziface.IConnection, roleid int64) pb.ErrorCodeManager {
	robot := s.LoadByRoleId(roleid)
	s.S2C_BuildRobotUpgradeSuc(conn, robot)
	return pb.ErrorCodeManager_SUCCESS
}

func (s *Buildrobot_info_service) S2C_BuildRobotUpgradeSuc(conn ziface.IConnection, robot *Cache_buildrobot_info) {
	msgS2C := &pb.Robot2BuildLvUpOverS2C{}
	if robot != nil {
		//看是否有升级中并且升级时间已经到的建筑
		//nowMilliTime := time.Now().UnixMilli()
		upSucBids := make(map[string]*pb.BuildRobotLvUpData, 0) //升级成功的建筑
		if robot.BuildData != nil {
			for bid, bv := range robot.BuildData {
				if bv.CheckUpgradeState(robot.GetRobotBySerid, false) {
					upSucBids[bid] = bv.ToPb_BuildRobotLvUpData(robot.GetRobotBySerid)
				}
			}
		}

		msgS2C.Builds = make([]*pb.BuildRobotLvUpData, 0)

		for bid, pbd := range upSucBids {
			delete(robot.BuildData, bid)
			msgS2C.Builds = append(msgS2C.Builds, pbd)
			build.UpdateBuildLevel(bid)
		}

		msgS2C.IdleRobots = robot.Pb_GetIdleRobot()
		defer func() {
			robot.Save_AllData()
		}()
	}

	if msgS2C != nil {
		zlog.Debugf("ProtocolManager_BuildRobotLvUpOver %v", msgS2C)
		defer func() {
			conn.SendBuffPBMsg(uint32(pb.ProtocolManager_BuildRobotLvUpOver), msgS2C)
		}()
	}
}
